package kr.nexmore.slmsample2.view.boardsample.secondpart;

import java.util.Iterator;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import kr.nexmore.slmsample2.dao.MyBoardDao;
import kr.nexmore.slmsample2.nebula_sample.AbstractExampleTab;
import kr.nexmore.slmsample2.service.board.BoardSampleService;
import kr.nexmore.slmsample2.uri.Bundleclass_Uri;
import kr.nexmore.slmsample2.uri.Part_Uri;
import kr.nexmore.slmsample2.uri.Topic_Uri;
import kr.nexmore.slmsample2.util.SWTResourceManager;
import kr.nexmore.slmsample2.vo.BoardVo;

import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.di.UIEventTopic;
import org.eclipse.e4.ui.model.application.MAddon;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.nebula.widgets.pagination.collections.PageResultContentProvider;
import org.eclipse.nebula.widgets.pagination.renderers.navigation.ResultAndNavigationPageLinksRendererFactory;
import org.eclipse.nebula.widgets.pagination.table.PageableTable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;

public class BoardPart extends AbstractExampleTab implements MouseListener {

	private TableViewer viewer;
	private PageableTable pageableTable;

	@Inject
	private EPartService partService;// 파트 관리

	@Inject
	private IEventBroker broker;
	
	private  MAddon addon;

	@Override
	@PostConstruct
	public Control createControl(Composite parent) {

		// 10페이지로 고정
		int pageSize = 10;
		parent.setLayout(null);
		pageableTable = new PageableTable(parent, SWT.NONE, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.FULL_SELECTION | SWT.V_SCROLL, pageSize,
				PageResultContentProvider.getInstance(), null, ResultAndNavigationPageLinksRendererFactory.getFactory());
		pageableTable.setBounds(0, 0, 553, 333);
		pageableTable.setBackground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
		GridData gridData = (GridData) pageableTable.getCompositeBottom().getLayoutData();
		gridData.horizontalAlignment = SWT.CENTER;
		gridData.verticalAlignment = SWT.CENTER;

		viewer = pageableTable.getViewer();
		viewer.setContentProvider(ArrayContentProvider.getInstance());
		viewer.setLabelProvider(new LabelProvider());

		Table table = viewer.getTable();
		table.addMouseListener(this);
		table.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false, 1, 1));
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		pageableTable.setPageLoader(BoardSampleService.getInstance());
		pageableTable.setCurrentPage(0);

		TableViewerColumn tableViewerColumn = new TableViewerColumn(pageableTable.getViewer(), SWT.NONE);
		TableColumn col_rownum = tableViewerColumn.getColumn();
		col_rownum.setWidth(100);
		col_rownum.setText("번호");
		tableViewerColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				BoardVo p = (BoardVo) element;

				return String.valueOf(p.getIdx());
			}
		});

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(pageableTable.getViewer(), SWT.NONE);
		TableColumn col_tag_id = tableViewerColumn_1.getColumn();
		col_tag_id.setWidth(100);
		col_tag_id.setText("제목");
		tableViewerColumn_1.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				BoardVo p = (BoardVo) element;
				return p.getTitle();
			}
		});

		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(pageableTable.getViewer(), SWT.NONE);
		TableColumn col_organization_name = tableViewerColumn_2.getColumn();
		col_organization_name.setWidth(100);
		col_organization_name.setText("이름");
		tableViewerColumn_2.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				BoardVo p = (BoardVo) element;
				return p.getName();
			}
		});

		Button button = new Button(parent, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				MPart part = partService.findPart(Part_Uri.PART_THIRD);
				partService.hidePart(part);
				part.setContributionURI(Bundleclass_Uri.INSERTPART);
				partService.activate(part);

			}
		});
		button.setText("등록");
		button.setBounds(10, 378, 76, 25);

		Button button_1 = new Button(parent, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();

				BoardVo vo = (BoardVo) selection.getFirstElement();
				if (vo == null) {// 선택하지 않앗을때
					MessageDialog.openWarning(e.display.getActiveShell(), "선택된 값이 없음", "해당 열이 선택되지 않았습니다.");

				} else {
					boolean answer = MessageDialog.openQuestion(e.display.getActiveShell(), "삭제하시겠습니까?", vo.getIdx() + "번째 게시글을 삭제하시겠습니까??");
					if (answer) {// yes를 눌럿을때
						MyBoardDao boardDao = new MyBoardDao();
						boardDao.delete(vo);
						broker.post(Topic_Uri.BOARD_PART_REFRESH, null);

					}
				}

			}
		});
		button_1.setText("삭제");
		button_1.setBounds(448, 378, 76, 25);

		pageableTable.refreshPage();// 갱신- 이게빠지면 처음에 값을 못가져옴

		return parent;
	}

	@Override
	public void createParameters(Composite parent) {

	}

	@Override
	public String[] createLinks() {

		return null;
	}

	@Override
	public void mouseDoubleClick(MouseEvent e) {

		IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();

		MPart part = partService.findPart(Part_Uri.PART_THIRD);
		partService.hidePart(part);
		part.setContributionURI(Bundleclass_Uri.UPDATEPART);
		partService.activate(part);
		BoardVo vo = (BoardVo) selection.getFirstElement();

		broker.post(Topic_Uri.THIRD_PART_UPDATE_SENDVO, vo);

		/*
		 * if (dialog.open() == InsertPart.OK) {// 다이얼로그가 등록되었을때
		 * pageableTable.refreshPage(); }
		 */

	}

	@Override
	public void mouseDown(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseUp(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Inject
	@Optional
	public void receiveEvent(@UIEventTopic(Topic_Uri.BOARD_PART_REFRESH) BoardVo vo) {

		pageableTable.refreshPage();

	}

	/*public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(800, 600));
		configurer.setShowCoolBar(false);
		configurer.setShowStatusLine(false);
		configurer.setTitle("RFID demo");

	}*/

}
